package com.credit.jumu.calculator.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.credit.jumu.calculator.R;
import com.credit.jumu.calculator.activity.MyWebActivity;
import com.credit.jumu.calculator.base.BaseFragment;
import com.credit.jumu.calculator.calculatorapp.CalculatorApp_;
import com.credit.jumu.calculator.utils.DataSharedPreference;
import com.credit.jumu.calculator.utils.LogUtils;
import com.credit.jumu.calculator.utils.MonitorOnBack;
import com.credit.jumu.calculator.utils.SystemUtil;
import com.credit.jumu.calculator.utils.ToastUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * Created by Administrator on 2017/11/22/022.
 */
@EFragment(R.layout.activity_web)
public class FindFragment extends BaseFragment {

	@ViewById(R.id.myallweb)
	public WebView webView;
	String url;
	@ViewById
	ImageView back;
	@ViewById
	TextView tv_title;
	@ViewById(R.id.webProgress)
	ProgressBar mypr;
	@ViewById(R.id.web_refresh)
	ImageView refresh;
	private DataSharedPreference ds;
	@ViewById
	TextView barheight;
	String myTitle = "";
	//声明内部定义的回调接口

	@AfterViews
	public void initView() {

		barheight.setVisibility(View.GONE);
		back.setVisibility(View.INVISIBLE);
		mypr.setMax(100);
		if (null == ds) {
			ds = new DataSharedPreference(getActivity());
		}
		ds.setUUID(SystemUtil.getAndroidId(getActivity().getApplicationContext()));
		url = ds.getRedirectUrl();
		LogUtils.println("zhangyiyi&&&&&" + url);
		if (TextUtils.isEmpty(url)) {
			ToastUtil.showShortToast("网络异常");
			return;
		}
		webSet();
		refresh.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				webView.reload();
			}
		});
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (null == webView) {
					return;
				}
				if (webView.canGoBack()) {
					goBack();
					CalculatorApp_.canGoBack = true;
				} else {
					CalculatorApp_.canGoBack = false;
				}
			}
		});
	}


	private void webSet() {
		WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
		webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
		webSettings.setSupportZoom(true);
		webSettings.setDisplayZoomControls(false);
		webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
		webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
		webSettings.setDomStorageEnabled(true); // 开启 DOM storage API 功能
		webSettings.setDatabaseEnabled(true);   //开启 database storage API 功能
		webSettings.setAppCacheEnabled(true);//开启 Application Caches 功能.
		webView.loadUrl(url);
		webView.setWebViewClient(new WebViewClient() {
			@Override
			public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
				handler.proceed();    //表示等待证书响应
				// handler.cancel();      //表示挂起连接，为默认方式
				// handler.handleMessage(null);    //可做其他处理
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				mypr.setVisibility(View.VISIBLE);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				mypr.setVisibility(View.GONE);
				tv_title.setText(myTitle);
				LogUtils.println("目前链接" + url);
				if (url.equals(ds.getRedirectUrl())) {
					//ToastUtil.showShortToast("是一级页面");
					back.setVisibility(View.INVISIBLE);
					CalculatorApp_.canGoBack = false;
				} else {
					//ToastUtil.showShortToast("不是一级页面");
					back.setVisibility(View.VISIBLE);
					CalculatorApp_.canGoBack = true;
				}
			}
		});
		webView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				mypr.setProgress(newProgress);
			}

			@Override
			public void onReceivedTitle(WebView view, String title) {
				super.onReceivedTitle(view, title);
				myTitle = title;
			}
		});
	}

	public void goBack() {
		if (null != webView) {
			webView.goBack();
		}
	}

}
